﻿using System;
using System.Linq;
using System.Collections.Generic;
using Domain.Core;
using Domain.MainModule.Entities;
using Domain.MainModule.RetailRates;

namespace Application.MainModule.RetailRateManagement
{
    public class RetailRateManagementService : IRetailRateManagementService, IDisposable
    {
        #region private properties

        private IRetailRateRepository RetailRateRepository
        {
            get;
            set;
        }

        private IRetailRateRepository RetailRateTypeRepository
        {
            get;
            set;
        }

        #endregion

        #region constructor

        /// <summary>
        /// Default constructor for RetailRateService.
        /// </summary>
        /// <param name="retailRateRepository">RetailRateRepository dependency, usually this is resolved by IoC</param>
        public RetailRateManagementService(IRetailRateRepository retailRateRepository)
        {
            if (retailRateRepository == (IRetailRateRepository)null)
            {
                throw new ArgumentNullException(Resources.Messages.exception_DependenciesAreNotInitialized);
            }

            this.RetailRateRepository = retailRateRepository;
        }

        #endregion

        #region IRetailRateService implementations

        public void AddRetailRate(RetailRate retailRate)
        {
            IUnitOfWork unitOfWork = this.RetailRateRepository.UnitOfWork as IUnitOfWork;
            this.RetailRateRepository.Add(retailRate);
            unitOfWork.Commit();
        }

        public void ChangeRetailRate(RetailRate retailRate)
        {
            IUnitOfWork unitOfWork = this.RetailRateRepository.UnitOfWork as IUnitOfWork;
            this.RetailRateRepository.Modify(retailRate);
            unitOfWork.CommitAndRefreshChanges();
        }

        public RetailRate FindRetailRateById(Guid retailRateId)
        {
            RetailRateIdSpecification retailRateIdSpecification = new RetailRateIdSpecification(retailRateId);
            return this.RetailRateRepository.FindRetailRate(retailRateIdSpecification);
        }

        public List<RetailRate> FindRetailRateByBranchId(Guid userId)
        {
            RetailRateBranchIdSpecification retailRateBranchIdSpecification = new RetailRateBranchIdSpecification(userId);
            return this.RetailRateRepository.FindRetailRates(retailRateBranchIdSpecification).ToList<RetailRate>();
        }

        public List<RetailRate> FindRetailRateByRateId(Guid rateId)
        {
            RetailRateRateIdSpecification retailRateRateIdSpecification = new RetailRateRateIdSpecification(rateId);
            return this.RetailRateRepository.FindRetailRates(retailRateRateIdSpecification).ToList<RetailRate>();
        }

        public void RemoveRetailRate(RetailRate retailRate)
        {
            if (retailRate == (RetailRate)null)
                throw new ArgumentNullException("retailRate");

            IUnitOfWork unitOfWork = this.RetailRateRepository.UnitOfWork as IUnitOfWork;
            this.RetailRateRepository.Remove(retailRate);
            unitOfWork.CommitAndRefreshChanges();

        }

        #endregion

        #region IDisposable implementations

        /// <summary>
        /// Dispose associated context with this domain service.
        /// </summary>
        public void Dispose()
        {
            if (this.RetailRateRepository != null
                &&
                this.RetailRateRepository.UnitOfWork != null)
            {
                this.RetailRateRepository.UnitOfWork.Dispose();
            }
        }

        #endregion
    }
}
